package com.hcy.rabbitruntimeapi.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author huchenying
 * @Description
 * @Date 2021/12/15 16:41
 */
@Component
public class CheckTokenAccess {

    Logger logger = LoggerFactory.getLogger(CheckTokenAccess.class);

    @Autowired
    RestTemplate restTemplate;

    @Value("${oauth2-server-url}")
    private String oauth2ServerUrl;

    public HashMap<String,String> checkToken(String token){
//        logger.info("验证token param -> {}",token);
        StringBuilder sb = new StringBuilder(oauth2ServerUrl);
        sb.append("/oauth/check_token?token=").append(token);
        HashMap res = null;
        String user_name = null;
        String client_id = null;
        try {
            res = restTemplate.getForObject(sb.toString(), HashMap.class);
            assert res != null : "结果为空！";
            user_name = (String) res.get("user_name");
            client_id = (String) res.get("client_id");
            logger.info("user_name = [{}],client_id = [{}]",user_name,client_id);
        } catch (RestClientException e) {
            logger.error("token验证失败！ result -> {}",e.getMessage());
            return null;
        }
        return res;
    }
}
